from odoo import models, fields, api, _
from odoo.exceptions import UserError, ValidationError


# 物料编码属性类
class MaterialCodingAttribute(models.Model):
    _name = 'sb.material.coding.attribute'
    _description = 'Material Coding Attribute'

    name = fields.Char(string="Attribute Name", translate=True)
    material_coding_attribute_value_ids = fields.One2many(
        'sb.material.coding.attribute.value', 'material_coding_attribute_id', string="Selectable Values")
    active = fields.Boolean(default=True, string="Active")
    coding_attribute_type = fields.Selection(
        string='Attribute Type',
        selection=[
            ('value', 'Selection'),
            ('input', 'Input'),
            ('desc_input', 'Description Input'),  # 描述输入
            ('partner', 'Supplier Coding'),
            ('department', 'Department Conding'),  # 部门编码
            ('plate_structure', 'Slab Structure'),
            ('flow_number', 'Flow number')]
        , default='value')
    input_length = fields.Integer(string="Input Value Length Limit", default=4)
    description_prefix = fields.Text('Encoding suffix', default="")
    # input_placeholder = fields.Text(string="Placeholder")
    code = fields.Char(string="code")

    def write(self, vals):
        if vals.get('code'):
            raise ValidationError(_('Modifying the property code will affect the program, please contact the developer to change'))
        res = super(MaterialCodingAttribute, self).write(vals)
        return res


# 物料编码属性值类
class MaterialCodingAttributeValue(models.Model):
    _name = 'sb.material.coding.attribute.value'
    _description = 'Material Coding Attribute Value'

    name = fields.Char(string="Value Name")
    name_en = fields.Char(string="Value Name English")
    sequence = fields.Integer(string='Sequence')
    value_code = fields.Char(string="Attribute Value Code")
    material_coding_attribute_id = fields.Many2one('sb.material.coding.attribute', string="Coding Attribute",
                                                   ondelete="cascade")
